import pygame
import sys
import os

from world import World
from menu import Menu

from pygame.locals import *

if __name__ == '__main__':
    #engine init
    pygame.mixer.init(44100, -16, 1, 4096)
    pygame.init()

    #initialize display
    window = pygame.display.set_mode((800, 600))
    pygame.display.set_caption('Breakout Clone')
    screen = pygame.display.get_surface()

    clock = pygame.time.Clock()

    def start_game():
        global active_state
        global world
        
        world.new_game()
        active_state = world
        
    def quit():
        sys.exit(0)
        
    menu_items = [ ("Start Game", start_game), ("Quit", quit) ]

    #game world
    world = World()

    menu = Menu(menu_items)
    active_state = menu

    #handle system events - or pass events to the relevant entity (menu or player)
    def handle_input_event(events):
        for event in events:
            if event.type == QUIT:
                quit()
            else:
                active_state.handle_input_event(event)

    #main game loop
    while True:
        clock.tick(60)
        handle_input_event(pygame.event.get())
        
        active_state.update()
        active_state.draw(screen)
        
        if active_state == world and world.closed:
                active_state = menu
        
        pygame.display.flip()